import { Component, OnInit, ViewChild, TemplateRef, OnDestroy } from '@angular/core';
import { Subscription } from 'rxjs/internal/Subscription';
import { RxStompService } from '@stomp/ng2-stompjs';
import { Message } from '@stomp/stompjs';
import { NzModalService, NzModalRef } from 'ng-zorro-antd';
import { Router, ActivatedRoute } from '@angular/router';
import { MonitorWarningTipComponent } from '../shared/components/monitor-warning-tip/monitor-warning-tip.component';
import { MonitorService } from '../core/service/monitor.service';
import { MonitorVo } from '../shared/models';
import { LoginService } from '../core/service/login.service';
import { AuthService } from '../core/auth/auth.service';
import { AUTH_LIST } from '../core/auth/role';

@Component({
  selector: 'app-layout',
  templateUrl: './layout.component.html',
  styleUrls: ['./layout.component.scss']
})
export class LayoutComponent implements OnInit, OnDestroy {
  @ViewChild('trigger') customTrigger: TemplateRef<void>;
  isCollapsed = false;
  triggerTemplate = null;
  username = '';

  private topicSubscription: Subscription;
  private modal: NzModalRef;

  toggleCollapsed(): void {
    this.isCollapsed = !this.isCollapsed;
  }

  constructor(
    private router: Router,
    private loginservice: LoginService,
    private rxStompService: RxStompService,
    private monitorService: MonitorService,
    private authService: AuthService,
    private modalService: NzModalService,
    private activatedRoute: ActivatedRoute
  ) {

  }

  logout() {
    console.log('退出');
    this.loginservice.logout().subscribe(() => {
      this.authService.clearUserInfo();
      this.router.navigate(['']);
    });
  }

  ngOnInit(): void {
    const isAllowSubjectMsg = this.authService.isAuthorized(AUTH_LIST.subject_warningmsg);
    if (isAllowSubjectMsg) {
      this.subjectWarningMsg();
    }
    this.activatedRoute.queryParams.subscribe(queryParams => {
      this.username = queryParams.username;
    });
  }

  private subjectWarningMsg() {
    this.topicSubscription = this.rxStompService.watch('/topic/WarningMessage')
      .subscribe((message: Message) => {
        const warningMsg = `[
          {
            "application": {"app_uuid":"0a6c65967aeb4d1f81dae869c72cdd05","id":17,"importance":0,"name":"spring-demo"},
            "assetType":"container",
            "cveInfo":{"name":"CVE-9999-0326"},
            "foundTime":1553570186000,
            "targetAssets":[{"name":"Apache Tomcat 9.0.0.M19","uuid":"c56e9d0d49f211e9b3fb005056bba4e1"}]
          }, {
            "application":{"app_uuid":"0a6c65967aeb4d1f81dae869c72cdd05","id":17,"importance":0,"name":"spring-demo"},
            "assetType":"container",
            "cveInfo":{"name":"CVE-9999-0326"},
            "foundTime":1553570186000,
            "targetAssets":[{"name":"Apache Tomcat 6.0.53","uuid":"c04454e8bca74b67a7beec2009bc04b2"}]
          }, {
            "application":{"app_uuid":"d1ca8f9ba09e493d806c0a8020902f07","id":18,"importance":0,"name":"vulhunter-demo"},
            "assetType":"container",
            "cveInfo":{"name":"CVE-9999-0326"},
            "foundTime":1553570186000,
            "targetAssets":[{"name":"Apache Tomcat 6.0.53","uuid":"c04454e8bca74b67a7beec2009bc04b2"}]
          }, {
            "application":{"app_uuid":"0a6c65967aeb4d1f81dae869c72cdd05","id":17,"importance":0,"name":"spring-demo"},
            "assetType":"jar",
            "cveInfo":{"name":"CVE-9999-0325"},
            "foundTime":1553570186000,
            "targetAssets":[{"name":"commons-beanutils-1.7.0","uuid":"5675fd96b29656504b86029551973d60fb41339b"}]
          }, {
            "application":{"app_uuid":"b495bd381878459684df1bc0e8a3f00e","id":20,"importance":0,"name":"weblogic11-demo"},
            "assetType":"jar",
            "cveInfo":{"name":"CVE-9999-0325"},
            "foundTime":1553570186000,
            "targetAssets":[{"name":"commons-beanutils-1.7.0","uuid":"5675fd96b29656504b86029551973d60fb41339b"}]
          }, {
            "application":{"app_uuid":"0a6c65967aeb4d1f81dae869c72cdd05","id":17,"importance":0,"name":"spring-demo"},
            "assetType":"jar",
            "cveInfo":{"name":"CVE-9999-0325"},
            "foundTime":1553570186000,
            "targetAssets":[{"name":"batik-util-1.7","uuid":"5c4dd0dd9a86a2fba2c6ea26fb62b32b21b2a61e"}]
          }, {
            "application":{"app_uuid":"b495bd381878459684df1bc0e8a3f00e","id":20,"importance":0,"name":"weblogic11-demo"},
            "assetType":"jar",
            "cveInfo":{"name":"CVE-9999-0325"},
            "foundTime":1553570186000,
            "targetAssets":[{"name":"batik-util-1.7","uuid":"5c4dd0dd9a86a2fba2c6ea26fb62b32b21b2a61e"}]
          }]`;
        const warningInfoArr: Array<MonitorVo> = JSON.parse(warningMsg);
        this.monitorService.monitorList = warningInfoArr;
        if (this.modal) {
          this.modal.destroy();
        }
        this.openModal();
      });
  }

  private openModal() {
    this.modal = this.modalService.create({
      nzTitle: 'Modal Title',
      nzContent: MonitorWarningTipComponent,
      nzMaskClosable: false,
      nzMask: false,
      nzWidth: 400,
      nzFooter: null
    });
  }

  ngOnDestroy() {
    if (this.topicSubscription) {
      this.topicSubscription.unsubscribe();
    }
  }
}
